package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.dao.DmsValueDao;
import com.ruoyi.system.domain.dto.DmsValueDto;
import com.ruoyi.system.domain.po.BlsZlInfo;
import com.ruoyi.system.domain.po.BlsZlInfoRecord;
import com.ruoyi.system.domain.vo.DmsValueVo;
import com.ruoyi.system.service.TbDataValueService;
import com.yuanxiang.treatment.param.analyzing.utils.AnalyzingParamsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.enums.DataSourceType.SLAVE;

@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public class TbDataValueServiceImpl implements TbDataValueService {

    @Resource
    private DmsValueDao dmsValueDao;

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Zyblgy> queryZyblgy(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryZyblgy(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Rybljl> queryRybljl(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryRybljl(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Swjlxx> querySwjlxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.querySwjlxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Gzhcsyjlxx> queryGzhcsyjlxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryGzhcsyjlxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Gzhcsyjlhc> queryGzhcsyjlhc(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryGzhcsyjlhc(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Yizhu> getYizhu(@Param("hzlshList") List<String> hzlshList) {
        return dmsValueDao.getYizhu(hzlshList);
    }

    @DataSource(DataSourceType.SLAVE)
    @Override
    public List<DmsValueVo.Hrcyjlxx> query24Hrcyjlxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.query24Hrcyjlxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Scbcjlxx> queryScbcjlxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryScbcjlxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Lcljjlxx> queryLcljjlxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryLcljjlxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Cyxj> queryCyxj(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryCyxj(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Jybgzb> queryJybgzb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryJybgzb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Jyjgzbb> queryJyjgzbb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryJyjgzbb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Xjjgb> queryXjjgb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryXjjgb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Ymjgb> queryYmjgb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryYmjgb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Jywdcj> queryJywdcj(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryJywdcj(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Jcwdcj> queryJcwdcj(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryJcwdcj(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Jbxx> queryJbxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryJbxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Fzxx> queryFzxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryFzxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Zdxx> queryZdxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryZdxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Patient> queryHzxx(List<DmsValueDto.Hzxx> hzxxList) {
        return dmsValueDao.queryHzxx(hzxxList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Ssxx> querySsxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.querySsxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.TbZyRydjmxb> queryTbZyRydjmxb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryTbZyRydjmxb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.TbZyJsmxb> queryTbZyJsmxb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryTbZyJsmxb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.TbZyCydjmxb> queryTbZyCydjmxb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryTbZyCydjmxb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.TbBlMzblgy> queryTbBlMzblgy(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryTbBlMzblgy(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.TbSsSsmxb> queryTbSsSsmx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryTbSsSsmx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Fyxx> queryFyxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryFyxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Icuxx> queryIcuxx(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryIcuxx(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Zyrb> queryZyrb(Date startDate, Date endDate, List<String> jzlshList) {
        return dmsValueDao.queryZyrb(startDate, endDate, jzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.MtProgress> queryMtProgress(List<String> jzlshList) {
        return dmsValueDao.queryMtProgress(jzlshList);
    }

    @DataSource(value = SLAVE)
    @Override
    public List<BlsZlInfoRecord> getMtPatientCode(BlsZlInfo blsZlInfo) {
        log.error("000");
        List<DmsValueVo.MtProgress> mtProgresses = dmsValueDao.queryMtProgress(CollectionUtil.newArrayList(blsZlInfo.getJzlsh()));
        if (CollectionUtil.isEmpty(mtProgresses)) {
            return new ArrayList();
        }
        log.error("111");
        List<String> treatmentType = new ArrayList<>();
        if (StrUtil.isNotEmpty(blsZlInfo.getSsbm())) {
            List<String> ssbmList = Arrays.stream(blsZlInfo.getSsbm().split(",")).collect(Collectors.toList());
            for (String ssbm : ssbmList) {
                if (UserConstants.CEA.contains(ssbm)) {
                    treatmentType.add("CEACAS");
                }
                if (UserConstants.CAS.contains(ssbm)) {
                    treatmentType.add("CEACAS");
                }
                if (UserConstants.AISJMRS.contains(ssbm)) {
                    treatmentType.add("JMRS");
                }
                if (UserConstants.AISJRZT.contains(ssbm)) {
                    treatmentType.add("JRZT");
                }
                if (UserConstants.NCX.contains(ssbm)) {
                    treatmentType.add("NCX");
                }
                if (UserConstants.LNDML.contains(ssbm)) {
                    treatmentType.add("DML");
                }
            }
        }
        log.error("222");
        //患者文书
        List<Map<String, String>> progressList = new ArrayList<>();
        try {
            for (DmsValueVo.MtProgress mtProgress : mtProgresses) {
                if (StrUtil.isEmpty(mtProgress.getProgressType())) {
                    log.error("mtProgress.getProgressType() is null");
                    continue;
                }
                if (mtProgress.getProgressType().matches(".*静脉溶.*|.*溶栓.*")) {
                    treatmentType.add("JMRS");
                }
                Map<String, String> map = new HashMap<>();
                map.put("progress_type", mtProgress.getProgressType());
                map.put("progress_template_name", mtProgress.getProgressTemplateName());
                map.put("progress_message", mtProgress.getProgressMessage().toString());
                progressList.add(map);
            }
        } catch (Exception e) {
            log.error("e:{}", e.getMessage());
        }
        log.error("333");
        List<BlsZlInfoRecord> blsZlInfoRecordList = new ArrayList<>();
        try {
            if (CollectionUtil.isEmpty(treatmentType)) {
                return blsZlInfoRecordList;
            }
            treatmentType = treatmentType.stream().distinct().collect(Collectors.toList());
            for (String s : treatmentType) {
                Map<String, String> jmrs = AnalyzingParamsUtil.getParamsByTypeAndProgress(s, progressList);
                if (CollectionUtil.isNotEmpty(jmrs)) {
                    for (Map.Entry<String, String> entry : jmrs.entrySet()) {
                        BlsZlInfoRecord blsZlInfoRecord = new BlsZlInfoRecord();
                        blsZlInfoRecord.setDelFlag((byte) 0);
                        blsZlInfoRecord.setCode(entry.getKey());
                        blsZlInfoRecord.setAnswer(entry.getValue());
                        blsZlInfoRecord.setId(IdUtil.getSnowflake().nextId());
                        blsZlInfoRecord.setBlId(blsZlInfo.getId());
                        blsZlInfoRecordList.add(blsZlInfoRecord);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("e:{}", e.getMessage());
        }
        log.error("解析完成：{}", blsZlInfoRecordList);
        return blsZlInfoRecordList;
    }
}
